package com.zsl.swing.redis.desktop.window.list.oftenusekey;

import com.zsl.swing.redis.desktop.model.NodeEntity;
import com.zsl.swing.redis.desktop.type.MainMenuEnum;
import com.zsl.swing.redis.desktop.utils.CollectionUtils;
import com.zsl.swing.redis.desktop.utils.ConfigUtils;
import com.zsl.swing.redis.desktop.utils.DialogUtils;
import com.zsl.swing.redis.desktop.utils.OffUseKeyFileUtils;
import com.zsl.swing.redis.desktop.window.list.ZslListModel;
import com.zsl.swing.redis.desktop.window.menu.ZslMenuItem;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

public class ZslOfenUseKeyPatternList extends JList<OftenUseKeyPatternListNode> {

    private final NodeEntity dbEntity;

    private final JTextArea queryTextField;


    private static final int MAX_OFTEN_USE_KEY_LIMIT = 200;

    public  ZslOfenUseKeyPatternList(NodeEntity dbEntity, JTextArea queryTextField){

        Comparator<OftenUseKeyPatternListNode> comparator = this.buildOftenUseKeyComparator();

        ZslListModel<OftenUseKeyPatternListNode> model = new ZslListModel<>(comparator);
        this.setModel(model);

        this.dbEntity = dbEntity;
        this.queryTextField = queryTextField;

        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        this.addListSelectionListener(this::doForSelect);
        
        this.initListData();
        
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                int type = e.getButton();
                if(type != MouseEvent.BUTTON3){
                    return;
                }

                int index = ZslOfenUseKeyPatternList.this.locationToIndex(e.getPoint());
                OftenUseKeyPatternListNode node = null;
                try{
                    node = model.getElementAt(index);
                }catch (Exception e1){
                    e1.printStackTrace();
                }

                if(Objects.isNull(node)){
                    return;
                }

                final OftenUseKeyPatternListNode targetNode = node;
                ZslMenuItem d = ZslMenuItem.getInstance(MainMenuEnum.DELETE.getText(), event ->ZslOfenUseKeyPatternList.this.removeElementAction(targetNode));

                JPopupMenu popupMenu = new JPopupMenu();
                popupMenu.add(d);

                popupMenu.show(ZslOfenUseKeyPatternList.this, e.getX(), e.getY());
            }
        });
    }

    private void removeElementAction(OftenUseKeyPatternListNode targetNode) {
        ZslListModel<OftenUseKeyPatternListNode> model = (ZslListModel<OftenUseKeyPatternListNode>) this.getModel();

        model.removeOneElement(targetNode);

        this.refresh();
    }

    private Comparator<OftenUseKeyPatternListNode> buildOftenUseKeyComparator() {

        return (o1,o2) -> o2.getUpdateMils() - o1.getUpdateMils() >=0L ? 1: -1;
    }

    private void initListData() {

        ZslListModel<OftenUseKeyPatternListNode> model = (ZslListModel<OftenUseKeyPatternListNode>) this.getModel();

        model.removeAllElementList();

        List<OftenUseKeyPatternListNode> list = OffUseKeyFileUtils.readListFromFile(this.dbEntity.getUniqueId(), this.dbEntity.getDbIndex());

        if(!CollectionUtils.isEmpty(list)){
            model.addElementList(list);
        }
    }

    private void doForSelect(ListSelectionEvent e) {
        OftenUseKeyPatternListNode selectedNode = this.getSelectedValue();

        if(Objects.isNull(selectedNode)){
            return ;
        }
        
        this.queryTextField.setText(selectedNode.getOftenUseKeyPattern());
        selectedNode.setUpdateMils(System.currentTimeMillis());

        this.refresh();
        this.updateUI();
    }

    private void refresh(){
        int dbIndex = this.dbEntity.getDbIndex();
        String uniqueId = this.dbEntity.getUniqueId();

        ZslListModel<OftenUseKeyPatternListNode> model = (ZslListModel<OftenUseKeyPatternListNode>) this.getModel();

        List<OftenUseKeyPatternListNode> currentNodeList = model.copyCurrentList();
        OffUseKeyFileUtils.writeListToFile(uniqueId, dbIndex, currentNodeList);

        this.setModel(model);
    }


    public void addNode(String key) {
        ZslListModel<OftenUseKeyPatternListNode> model = (ZslListModel<OftenUseKeyPatternListNode>) this.getModel();

        int size = model.getList().size();
        if(size > MAX_OFTEN_USE_KEY_LIMIT){
            DialogUtils.errorDialog("常用key集最多支持200个，请删除不常用的后继续添加!");
            return;
        }

        int dbIndex = this.dbEntity.getDbIndex();
        String uniqueId = this.dbEntity.getUniqueId();


        OftenUseKeyPatternListNode current = new OftenUseKeyPatternListNode();

        current.setUpdateMils(System.currentTimeMillis());
        current.setOftenUseKeyPattern(key);
        current.setDbIndex(dbIndex);
        current.setUniqueId(uniqueId);

        model.removeOneElement(current);

        model.addOneElement(current);

        List<OftenUseKeyPatternListNode> currentNodeList = model.copyCurrentList();
        OffUseKeyFileUtils.writeListToFile(uniqueId, dbIndex, currentNodeList);

        this.setModel(model);


    }
}
